/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-10-28 14:48:02   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.service;

import java.time.OffsetDateTime;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zx.cloud.domain.ClientAuth;
import com.zx.cloud.domain.ClientInfo;
import com.zx.cloud.persistence.ClientAuthMapper;
import com.zx.cloud.persistence.ClientInfoMapper;
import com.zx.cloud.persistence.CrudMapper;
import com.zx.cloud.util.Page;

/**
 * 会员详细信息
 */
@Service
@Transactional
public class ClientInfoService extends CrudServiceImpl<ClientInfo>{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ClientInfoService.class);
    
    @Autowired
    private ClientInfoMapper clientInfoMapper;
    @Autowired
	private ClientAuthService clientAuthService;
    @Autowired
    private ClientAuthMapper clientAuthMapper;
    
  	@Autowired
    public ClientInfoService(CrudMapper<ClientInfo> mapper) {
        super(mapper);
    }
  	
  	public ClientInfo getClientInfoById(String tableId){
  		
  		return clientInfoMapper.findClientMoreMsg(tableId);
  	}
  	
  	public Page<ClientInfo> findClientAuth(ClientInfo clientInfo){
  		
		long count = clientInfoMapper.findClientAuthMsgCount(clientInfo);
		Page<ClientInfo> page = new Page<ClientInfo>(clientInfo.getPageIndex_(), clientInfo.getPageSize_());
		page.setData(clientInfoMapper.findClientAuthMsg(clientInfo));
		page.setTotalCount(count);
		
		return page;
  	}
  	
  	public int handleClientUpdate(ClientInfo clientInfo){
  		
  		ClientInfo insEntity = clientInfoMapper.findClientById(clientInfo.getClientId());
  		clientInfo.setId(insEntity.getId());
  		super.update(clientInfo);
  		
  		if(null != insEntity){
  			//修改个人信息认证状态--填写了表示已认证
  			ClientAuth clientAuth = clientAuthMapper.findByClientID(clientInfo.getClientId());
  			clientAuth.setPrivateMsg(1);
  			clientAuth.setPrivateTime(OffsetDateTime.now());
  			//判断是否全部认证
	  		if(clientAuth.getContactState() == 1 && clientAuth.getBankCardState() == 1 && 
	  				clientAuth.getIdState() == 1 && (clientAuth.getJobState() == 1 || clientAuth.getSchoolState() == 1)){
	  			clientAuth.setFlag(1);
	  		}
  			
  			String job = clientInfo.getJob();
  			if(StringUtils.isNotEmpty(job)){ //页面对职业进行处理
  				clientAuth.setJobState(1);
  				clientAuth.setJobTime(OffsetDateTime.now());
  		  		if(clientAuth.getContactState() == 1 && clientAuth.getBankCardState() == 1 && 
  		  				clientAuth.getPrivateMsg() == 1 && clientAuth.getIdState() == 1){
  		  			clientAuth.setFlag(1);
  		  		}
  			}
  			
  			String school = clientInfo.getSchool();
  			if(StringUtils.isNotEmpty(school)){ //页面对学校进行处理
  				clientAuth.setSchoolState(1);
  				clientAuth.setSchoolTime(OffsetDateTime.now());
  		  		if(clientAuth.getContactState() == 1 && clientAuth.getBankCardState() == 1 && 
  		  				clientAuth.getPrivateMsg() == 1 && clientAuth.getIdState() == 1){
  		  			clientAuth.setFlag(1);
  		  		}
  			}
  			
  	  		clientAuthService.update(clientAuth);	
  		}
  		
  		return 0;
  	}
  	
  
}
